package com.api.shopapi.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author maochuan
 */
public class BatchPushUtil {

//    private static String appId = "bYHBcHiOlMAzy5AZgulcm7";
//    private static String appKey = "APWml3lVsi6NVlpfS4Spp3";
//    private static String masterSecret = "LXOGIT763iAsGOgxktreO6";
//
//    static String CID_A = "9c9e91a4ffa8b84c3fab1b5c6656d9e4";

    private static String appId = "KamtnQFnTf8y9dIaRQg0s2";
    private static String appKey = "uDsUMXgo8x7R2NUVuQPU93";
    private static String masterSecret = "Is0tKdx46m8S4OjBwDNEC1";

    static String CID1 = "4cdec86a4a9ac52dab054cf6508a5696";
    static String CID2 = "4cdec86a4a9ac52dab054cf6508a5696";

    // 如果需要使用HTTPS，直接修改url即可
    static String host = "https://api.getui.com/apiex.htm";
    // static String host = "http://api.getui.com/apiex.htm";

    public static IPushResult sendMessage(List<String> cidarr,JSONObject jsonObject){
        System.setProperty("gexin_pushList_needDetails", "true");
        // 配置返回每个别名及其对应cid的用户状态，可选
        // System.setProperty("gexin_pushList_needAliasDetails", "true");
        IGtPush push = new IGtPush(host, appKey, masterSecret);
        // 通知透传模板
        TransmissionTemplate template = notificationTemplateDemo(jsonObject);
        ListMessage message = new ListMessage();
        message.setData(template);
        // 设置消息离线，并设置离线时间
        message.setOffline(true);
        // 离线有效时间，单位为毫秒
        message.setOfflineExpireTime(24 * 1000 * 3600);
        // 厂商通道下发策略
        message.setStrategyJson("{\"default\":4,\"ios\":4,\"st\":4}");
        List targets = new ArrayList();
        for (Object i:cidarr){
            String cid = (String) i;
            Target target = new Target();
            target.setAppId(appId);
            target.setClientId(cid);
            targets.add(target);
        }
        String taskId = push.getContentId(message);
        IPushResult ret = push.pushMessageToList(taskId, targets);
        //System.out.println(ret.getResponse().toString());
        return ret;

    }

    public static TransmissionTemplate notificationTemplateDemo(JSONObject jsonObject) {
        TransmissionTemplate template = new TransmissionTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);

        // 透传消息接受方式设置，1：立即启动APP，2：客户端收到消息后需要自行处理
        template.setTransmissionType(2);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("MsgID", 12812);
//        jsonObject.put("MsgType", 3);
//        jsonObject.put("MsgContent", "明天到大队来开会");
//        jsonObject.put("AskIsHear", 1);
//        jsonObject.put("AskIsJoin", 1);
        template.setTransmissionContent(String.valueOf(jsonObject));

        //template.setAPNInfo(getAPNPayload()); //详见【推送模板说明】iOS通知样式设置
        return template;
    }
}
